<?php
#                                                                                 
# author: Cody Sorgenfrey                                       
# (c)2012 South Hill Calvary Chapel                                               
#                                                                                 
include_once("./root.inc.php");
include_once("${root}/mysql/database_info.php");
include_once("${root}/mysql/common.php");
include_once("${root}/common.php");

function printEmailForm(){
	
	printf("<form action='needsPosted.php'>\n");
	
	printf("\t<br><input type='text' name='email' placeholder='Your Email' value='%s' />\n", $_GET['email']);	
	
	printf("\t<br><textarea name='message' style='width:500px; height:200px;'>Your message.</textarea>\n");	

	printf("\t<input type='hidden' name='id' value='%s' />\n", $_GET['id']);	

	printf("\t<input type='hidden' name='email' value='%s' />\n", $need['email']);	

	printf("\t<br><input type='submit' value='Send' />\n");
	
	printf("</form>\n");
}

function sendReply($email){
	
	return mail($email, "Your post on the SHCC Needs Board", $_GET['message'], "From: ".$_GET['email']);
}

$title = $need['title'];
$meta_key=null;
$meta_desc=null;
print_html_head($title ,$meta_key,$meta_desc);
print_java_head(null);

begin_body();
page_title($title);

if ($_REQUEST['needsLogin'] == 1){

	if ($_GET['id']){
		
		connect_select_test("needs");
		
		$result = mysql_query("SELECT * FROM `needs` WHERE `id`='".$_GET['id']."' && `pending`='0'");
		
		$need = mysql_fetch_array($result);
		
		if (empty($need)){
			
			printf("Sorry the post you're looking for no longer exists or hasn't been approved yet.");
		} 
		else {
			
			$postDate = date("D, M jS, Y", strtotime($need['postDate']));
			
			printf("<p>Posted on: %s<br><br>%s</p></div>\n", $postDate, $need['summary']);
			
			printf("<div class='content'><h4>Can you help?</h4>\n");
					
			if ($_GET['email'] && $_GET['message']){
				
				if(sendReply($need['email'])){
					
					printf("Thank you!");
				}
				else{
					
					printf("Sorry, there was an issue sending your message, please try again.");
					
					printEmailForm();
				}
			} 
			else{
				
				printEmailForm();
			}
		}
	}
	else{
	
		printf("<script type='text/javascript'>window.location = '/needs.php';</script>");	
	}
}
else{

	$vars = array("id", $_GET['id']);
		
	printNeedsLogin("/needsPosted.php?id=".$_GET['id']."", $vars);
}
end_page_title();
end_body();
?>